package com.gxp.controller.order;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gxp.pojo.Game;
import com.gxp.pojo.GameGoods;
import com.gxp.pojo.GameServer;
import com.gxp.pojo.Order;
import com.gxp.service.GameService;
import com.gxp.service.OrderService;
import com.gxp.util.common.GXPConstant;
import com.gxp.util.databind.DateEditor;

@Controller
public class PublicOrderController {
	
	Logger logger = Logger.getLogger(PublicOrderController.class);

	private String userId;
	
	@Autowired
	private GameService gameService;
	
	@Autowired
	private OrderService orderSerivce;

	@InitBinder
	protected void initBinder(HttpServletRequest request,
	                              ServletRequestDataBinder binder) throws Exception {
	    binder.registerCustomEditor(Date.class, new DateEditor());
	}
	
	@ModelAttribute
    public void getUserId() {
		UserDetails userDetail = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		userId = userDetail.getUsername();
		logger.debug("===========================curent user is : " + userId);
    }
	
	@RequestMapping(value = "/public/order/addOrder", method = RequestMethod.GET)
	public ModelAndView addOrder() {
		System.out.println("PublicOrderController addOrder is invoked.........");
		
		List<Game> games = gameService.getGames();
		ModelAndView mav = new ModelAndView();
		mav.addObject("games", games);
		mav.setViewName("public/order/addBuyerOrder");
		
		return mav;
	}
	
	@RequestMapping(value="/public/order/insertOrder",method=RequestMethod.POST)
	@ResponseBody
	public String insertOrder(Order order) {
		order.setCreateDt(new Date());
		orderSerivce.insertOrder(order);
		
		return "success";
	}
	
	@RequestMapping(value = "/public/order/manageOrder", method = RequestMethod.GET)
	public ModelAndView manageOrder() {
		System.out.println("OrderController manageBuyerGoods is invoked.........");
		
		List<Order> orders = orderSerivce.findByUserId(userId);
		ModelAndView mav = new ModelAndView();
		mav.addObject("orders", orders);
		mav.setViewName("public/order/manageBuyerOrder");
		
		return mav;
	}
	@RequestMapping(value = "/public/buyerOrder/edit/{orderId}", method = RequestMethod.GET)
	public ModelAndView editOrder(@PathVariable int orderId) {
		Order order = orderSerivce.findByOrderId(orderId);
		ModelAndView mav = new ModelAndView();
		if(userId.equals(order.getUserId())) {
			List<Game> games = gameService.getGames();
			int gameId = order.getGameId();
			List<GameServer> gameServers = gameService.getGameServers(gameId);
			List<GameGoods> gameGoods = gameService.getEquipments(gameId);
			mav.addObject("order", order);
			mav.addObject("games", games);
			mav.addObject("gameServers", gameServers);
			mav.addObject("gameGoods", gameGoods);
			logger.debug("game goods size: " + gameGoods.size());
			mav.setViewName("public/order/editBuyerOrder");
		} else {
			mav.setViewName("redirect:/public/order/manageOrder");
		}	
		
		return mav;
	}
	
	@RequestMapping(value="/public/buyerOrder/update",method=RequestMethod.POST)
	@ResponseBody
	public String updateOrder(Order order) {
		order.setUserId(userId);
		order.setModifiedDt(new Date());
		orderSerivce.updateBuyerOrder(order);
		return "success";
	}
	
	@RequestMapping(value="/public/game/server/get",method=RequestMethod.POST)
	@ResponseBody
	public List<GameServer> getGameServer(Integer gameId) {
		logger.debug("PublicOrderController getGameServer is invoked.........");
		List<GameServer> gameServers = gameService.getGameServers(gameId);
		logger.debug("gameServers size: " +  gameServers.size());		
		return gameServers;		
	}
	
	@RequestMapping(value="/public/goods/get",method=RequestMethod.POST)
	@ResponseBody
	public List<GameGoods> getGoods(Integer gameId) {
		logger.debug("PublicOrderController getGoods is invoked.........");
		List<GameGoods> goods = gameService.getEquipments(gameId);
		logger.debug("goods size: " +  goods.size());		
		return goods;		
	}
	
	@RequestMapping(value="/public/buyerOrder/view/{orderId}",method=RequestMethod.POST)
	@ResponseBody
	public Order viewOrderInfo(@PathVariable int orderId) {
		Map<String,Object> param = new HashMap<String, Object>();
		param.put(GXPConstant.MAPPER_KEY_ORDER_ID, orderId);
		param.put(GXPConstant.MAPPER_KEY_USER_ID, userId);	
		Order order = orderSerivce.findByOrderId(orderId);
		return order;
	}
	
	@RequestMapping(value="/public/buyerOrder/delete/{orderId}",method=RequestMethod.POST)
	@ResponseBody
	public String delOrder(@PathVariable int orderId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put(GXPConstant.MAPPER_KEY_USER_ID, userId);
		param.put(GXPConstant.MAPPER_KEY_ORDER_ID, orderId);
		orderSerivce.delOrder(param);
		return "success";
	}
}

